package com.kkb.kkbadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.kkbadmin.dao.CarouselMapper;
import com.kkb.kkbadmin.domain.Carousel;
import com.kkb.kkbadmin.service.CarouselService;
import com.kkb.kkbcommon.exception.Asserts;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杜垣均
 * @title: CarouselServiceImpl
 * @projectName kkb-parent-v2
 * @description 轮播图后台管理
 * @data 19:30 2021/8/19
 */
@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {


    @Override
    public PageInfo<Carousel> selectCarouselList(Carousel carousel, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Carousel> list = this.lambdaQuery().eq(ObjectUtil.isNotNull(carousel.getId()), Carousel::getId, carousel.getId())
                .eq(ObjectUtil.isNotNull(carousel.getState()), Carousel::getState, carousel.getState())
                .eq(ObjectUtil.isNotNull(carousel.getUpdateTime()), Carousel::getUpdateTime, carousel.getUpdateTime())
                .list();

        return  new PageInfo<>(list);
    }


    @Override
    public void addCarousel(Carousel carousel) {
        boolean save = this.save(carousel);

        if (!save) {
            Asserts.fail("添加失败");
        }
    }


    @Override
    public void updateCarousel(Carousel carousel) {
        boolean update = this.updateById(carousel);
        if (!update) {
            Asserts.fail("修改失败，可能不存在该id");
        }
    }


    @Override
    public void deleteCarousel(Long id) {
        boolean remove = this.removeById(id);
        if (!remove) {
            Asserts.fail("删除失败，可能不存在该id");
        }
    }
}
